package com.zzyl.nursing.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.thread.UserThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.mapper.MemberReservationMapper;
import com.zzyl.nursing.service.MemberReservationService;
import com.zzyl.nursing.vo.countByTimeVo;
import com.zzyl.serve.domain.NursingProject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class MemberReservationServiceImpl extends ServiceImpl<MemberReservationMapper, Reservation> implements MemberReservationService {
    @Autowired
    private MemberReservationMapper memberReservationMapper;

    @Override
    public int getCancelledReservationCount() {
        Long userId = UserThreadLocal.getUserId();
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        int count = memberReservationMapper.getCancelledReservationCount(userId, startTime, endTime);
        return count;
    }

    @Override
    public List<countByTimeVo> getCountByTime() {
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        return memberReservationMapper.getCountByTime(startTime, endTime);
    }

    @Override
    public boolean addReservation(Reservation reservation) {
        reservation.setStatus(0);
        Long userId = UserThreadLocal.getUserId();
        reservation.setCreateBy(String.valueOf(userId));
        boolean flag = this.save(reservation);
        return flag;
    }

    @Override
    public List<Reservation> getReservationPage(Integer status) {
        return memberReservationMapper.getReservationPage(status);
    }

    @Override
    public boolean cancelReservationById(Long id) {
        int count = getCancelledReservationCount();
        if (count > 3) {
            return false;
        }
        Reservation reservation = this.getOne(Wrappers.<Reservation>lambdaUpdate()
                .eq(Reservation::getId, id));
        reservation.setStatus(2);
        this.updateById(reservation);
        return true;
    }

    @Override
    public void updateReservationStatus() {
        //查询状态为0且预约时间小于当前时间的预约
        List<Reservation> list = this.list(Wrappers.<Reservation>lambdaQuery()
                .eq(Reservation::getStatus, 0)
                .le(Reservation::getTime, LocalDateTime.now()));
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        list.forEach(reservation -> {
            reservation.setStatus(3);
        });
        this.updateBatchById(list);
    }
}
